
# -*- coding: utf-8 -*-

from openerp.osv import orm
from openerp.addons.web.http import request
import werkzeug

SUPERUSER_ID = 1


# 拦截所有HTTP请求，检查是否有微信回调验证参数
# 有验证参数的话则进行验证，验证后跳转到业务页面
class ir_http(orm.AbstractModel):
    _inherit = 'ir.http'

    def _check_login(self):
        if request.session and request.session.get('login'):
            return True

        return False

    def _dispatch(self):

        # 检测是否是商城相关的链接
        path = request.httprequest.path
        if request.httprequest.path and path.startswith('/mshop') and not path.endswith('get_enterprise_info') and not self._check_login():
            if 'login' in request.params:
                return werkzeug.utils.redirect("http://" + request.httprequest.host + "/weixin/binding")

            if request.httprequest.headers.get('X-Requested-With') == 'XMLHttpRequest':
                raise werkzeug.exceptions.SecurityError()

        return super(ir_http, self)._dispatch()
